package com.data.modules.smzh.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.data.common.constant.enums.ProductEnum;
import com.data.common.util.KamliClientUtil;
import com.data.modules.sm.entity.SmProject;
import com.data.modules.sm.service.ISmProjectService;
import com.data.modules.smzh.entity.Building;
import com.data.modules.smzh.entity.Devicebuild;
import com.data.modules.smzh.entity.SmPushMsg;
import com.data.modules.smzh.mapper.SmPushMsgMapper;
import com.data.modules.smzh.service.IBuildingService;
import com.data.modules.smzh.service.IDevicebuildService;
import com.data.modules.smzh.service.ISmPushMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * @Description: 时迈推送消息
 * @Author: data-boot
 * @Date:   2023-02-07
 * @Version: V1.0
 */
@Service
public class SmPushMsgServiceImpl extends ServiceImpl<SmPushMsgMapper, SmPushMsg> implements ISmPushMsgService {

    @Autowired
    private ISmProjectService projectService;

    @Autowired
    private IDevicebuildService devicebuildService;

    @Autowired
    private IBuildingService buildingService;

    @Override
    public void pushMsg(String ids, String model, String projectNo) {


        SmProject smProject = projectService.getById(projectNo);
        String serialNo =smProject.getAppId()+smProject.getProjectNo();

        List mapList = new ArrayList();
        Map map = new HashMap<>();
        map.put("msg","notice");
        mapList.add(map);
        String comd = "noticeBindMangeBaselnfo";

        if(ObjectUtil.equals(ProductEnum.SMW_721M.getCode(),model)){//管理机

          List<Devicebuild> list = devicebuildService.lambdaQuery().eq(Devicebuild::getBuildingNo,serialNo)
                  .eq(Devicebuild::getType,2).list();

            list.stream().forEach(x->{

                KamliClientUtil.doPostpushMsgUr(x.getSerialNo(),comd,mapList);
            });
        }else{ //智慧屏

            List<Building> list = buildingService.lambdaQuery().eq(Building::getAppId,smProject.getAppId())
                    .eq(Building::getProjectNo,smProject.getProjectNo()).list();

            list.stream().forEach(x->{

                KamliClientUtil.doPostpushMsgUr(x.getUniqueNo(),comd,mapList);
            });

        }

        SmPushMsg smPushMsg = new SmPushMsg();
        smPushMsg.setId(ids);
        smPushMsg.setResult(1);
        smPushMsg.setUpdateTime(new Date());
        this.baseMapper.updateById(smPushMsg);

    }

}
